﻿using Outlook = Microsoft.Office.Interop.Outlook;
using Manager = OMC.DataManagement.Manager;

namespace OMC
{
    /// <summary>
    /// The main class contains the init, the start sequence and the destroy
    /// methods of the application. This class makes the application runnable.
    /// </summary>
    public partial class Main
    {
        private Outlook.Explorer explorer;
        private Outlook.Accounts accounts;
        private Manager manager { get; set; }

        /// <summary>
        /// This method initialize a manager and the handler of the application.
        /// </summary>
        /// <remarks>
        /// Source: http://msdn.microsoft.com/en-us/library/ms268750
        /// </remarks>
        private void Init()
        {
            //Initialize manager
            explorer = Application.Explorers.Application.ActiveExplorer();
            accounts = Application.Session.Accounts;
            manager = new Manager(accounts, explorer);

            //Initialize handler
            Application.ItemContextMenuDisplay
                += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(
                manager.contextMenuDisplay);
        }

        /// <summary>
        /// This Method cleans up before the application goes down.
        /// </summary>
        private void CleanUp()
        {
            //remove Handler
            Application.ItemContextMenuDisplay
                -= new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(
                manager.contextMenuDisplay);
        }

        #region Startup,Shutdown
        private void main_Startup(object sender, System.EventArgs e)
        {
            Init();
        }

        private void main_Shutdown(object sender, System.EventArgs e)
        {
            CleanUp();
        }
        #endregion

        #region VSTO generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(main_Startup);
            this.Shutdown += new System.EventHandler(main_Shutdown);
        }
        #endregion
    }
}